package com.gsx.service.Impl;

import com.github.pagehelper.PageHelper;
import com.gsx.entity.Comment;
import com.gsx.mapper.CommentMapper;
import com.gsx.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Value("${comment.avatar}")
    private String avatar;

    @Override
    public List<Comment> getCommentListById(Long blog_id) {
        /*将Comment分为两个层级*/
        List<Comment> resultList=new ArrayList<>();
        List<Comment> commentList=commentMapper.getCommentListById(blog_id);
        for(Comment comment1:commentList){
            List<Comment> temp = new ArrayList<Comment>();
            for (Comment comment2:commentList){
                if(comment2.TopParent_idIsNUll()==false){
                    if(comment1.getComment_id()==comment2.getTopParent_id()){
                        temp.add(comment2);
                    }
                }
            }
            comment1.setReplyComment(temp);
            if(comment1.TopParent_idIsNUll()){
                resultList.add(comment1);
            }
        }
        return resultList;
    }

    @Transactional
    public int saveComment(Comment comment){
        if(comment.getParentComment_id()==-1){
            comment.setParentComment_id(null);
        }
        if(comment.TopParent_idIsNUll()){
            comment.setTopParent_id(comment.getParentComment_id());
        }
        if(comment.getTopParent_id()==-1){
            comment.setTopParent_id(null);
        }
        comment.setComment_avatar(avatar);
        comment.setComment_createTime(new Date());
        return commentMapper.saveComment(comment);
    }

    public List<Comment> getCommentByBack(int page,int size,Comment comment){
        PageHelper.startPage(page,size);
        return commentMapper.getCommentByBack(comment);
    }

    public int deleteComment(Long id){
        return commentMapper.deleteComment(id);
    }
}
